﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BLL;
using Model;
using Maticsoft.BLL;

public partial class PersonInfo_GiftList : BasePage
{
    GiftBO gbo = new GiftBO();
    Access acc = new Access();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (GetQeuryString("para") == "GetList")
        {
            BindData();
        }
        else if (GetQeuryString("para") == "delete")
        {
            try
            {
                Response.Clear();
                string id = GetQeuryString("id");
                Gift gift = gbo.GetModel(int.Parse(id));
                gift.States = 3;
                gbo.Update(gift);
                Response.Write("ok");
            }
            catch (Exception ex)
            {
                Response.Write("Error" + ex.Message);
            }
            finally
            {
                Response.End();
            }

        }
        else if (GetQeuryString("para") == "excel")
        {
            try
            {
                string id = GetQeuryString("id");
                DataSet ds = acc.GetInventoryExcel(" gid=" + id);
                CreateExcel(ds,"库存明细");
            }
            catch (Exception ex)
            {
              //  Response.Write("Error" + ex.Message);
            }
            finally
            {
               // Response.End();
            }

        }
        else if (GetQeuryString("para") == "delete2")
        {
            try
            {
                Response.Clear();
                string id = GetQeuryString("id");
                Gift gift = gbo.GetModel(int.Parse(id));
                gift.States = 2;
                gbo.Update(gift);
                Response.Write("ok");
            }
            catch (Exception ex)
            {
                Response.Write("Error" + ex.Message);
            }
            finally
            {
                Response.End();
            }

        }

    }

    private void BindData()
    {
        Response.Clear();
        //string s = "{\"total\":\"239\",\"rows\":[  {\"code\":\"001\",\"name\":\"Name 1\",\"addr\":\"Address 11\",\"addr\":\"col4 data\"}   ]}  ";
        int PageNumber = Convert.ToInt32(Request.Params["page"] == null ? "1" : Request.Params["page"]) - 1;
        int pageSize = Convert.ToInt32(Request.Params["rows"] == null ? "10" : Request.Params["rows"]);
        string sort = " order by " + (Request.Params["sort"] == null ? "" : Request.Params["sort"]) + " " + (Request.Params["order"] == null ? " desc " : Request.Params["order"]);
        string condition = " 1=1  ";
        string GiftNo = Request.Params["GiftNo"] == null ? "" : Request.Params["GiftNo"];
        string Name = Request.Params["Name"] == null ? "" : Request.Params["Name"];
        string JF = Request.Params["JF"] == null ? "" : Request.Params["JF"];
        string K1 = Request.Params["K1"] == null ? "" : Request.Params["K1"];
        string K2 = Request.Params["K2"] == null ? "" : Request.Params["K2"];
        if (GiftNo != "")
            condition += " and SKU LIKE '%"+GiftNo+"%' ";
        if (Name != "")
            condition += " and Name LIKE '%" + Name + "%' ";
        if (JF != "")
            condition += " and Integral = " + JF + " ";
        if (K1 != "")
            condition += " and begin_inv>=" + K1 + " ";
        if (K2 != "")
            condition += " and begin_inv<=" + K2 + " ";
        DataSet ds = access.GetAllGift(sort, pageSize, PageNumber, condition);
        string re = ToJson.DataTableToJson(ds.Tables[0].Rows[0][0].ToString(), "rows", ds.Tables[1]);
        Response.Write(re);
        Response.End();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindData();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        int PageNumber = Convert.ToInt32(Request.Params["page"] == null ? "1" : Request.Params["page"]) - 1;
        int pageSize = Convert.ToInt32(Request.Params["rows"] == null ? "10" : Request.Params["rows"]);
        string sort = " order by " + (Request.Params["sort"] == null ? "" : Request.Params["sort"]) + " " + (Request.Params["order"] == null ? " desc " : Request.Params["order"]);
        //string condition = " 1=1 and IF_DISP=1 ";
        string condition = " 1=1  ";
        string GiftNo = Request.Params["GiftNo"] == null ? "" : Request.Params["GiftNo"];
        string Name = Request.Params["Name"] == null ? "" : Request.Params["Name"];
        string JF = Request.Params["JF"] == null ? "" : Request.Params["JF"];
        string K1 = Request.Params["K1"] == null ? "" : Request.Params["K1"];
        string K2 = Request.Params["K2"] == null ? "" : Request.Params["K2"];
        if (GiftNo != "")
            condition += " and SKU LIKE '%" + GiftNo + "%' ";
        if (Name != "")
            condition += " and Name LIKE '%" + Name + "%' ";
        if (JF != "")
            condition += " and Integral = " + JF + " ";
        if (K1 != "")
            condition += " and begin_inv>=" + K1 + " ";
        if (K2 != "")
            condition += " and begin_inv<=" + K2 + " ";
        DataSet ds = access.GetAllGiftExcel(condition);
        CreateExcel(ds,"商品");
    }
}